/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.safe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.common.constant.FlowDicConstant;
import com.hopes.bizz.basic.drive.dto.BaseAuditDTO;
import com.hopes.bizz.basic.safe.dto.BaseThreeMeantimeExcelDTO;
import com.hopes.bizz.basic.safe.dto.BaseThreeMeantimeFlowSearchDTO;
import com.hopes.bizz.basic.safe.entity.BaseThreeMeantimeEntity;
import com.hopes.bizz.basic.safe.entity.BaseThreeMeantimeHandleRecordEntity;
import com.hopes.bizz.basic.safe.service.BaseThreeMeantimeService;
import com.hopes.bizz.basic.safe.vo.BaseThreeMeantimeExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 项目三同时管理
 *
 * @author pig
 * @date 2023-10-30 09:59:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/meantime" )
@Tag(description = "meantime" , name = "项目三同时管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseThreeMeantimeController {

    private final BaseThreeMeantimeService baseThreeMeantimeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseThreeMeantime 项目三同时管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('safe_meantime_view')" )
    public R getbaseThreeMeantimePage(Page page, BaseThreeMeantimeEntity baseThreeMeantime) {
        LambdaQueryWrapper<BaseThreeMeantimeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseThreeMeantime.getOrgId()),BaseThreeMeantimeEntity::getOrgId,baseThreeMeantime.getOrgId());
        wrapper.eq(ObjectUtil.isNotNull(baseThreeMeantime.getProjectStatus()),BaseThreeMeantimeEntity::getProjectStatus,baseThreeMeantime.getProjectStatus());
        wrapper.eq(ObjectUtil.isNotNull(baseThreeMeantime.getProjectType()),BaseThreeMeantimeEntity::getProjectType,baseThreeMeantime.getProjectType());
        wrapper.ge(ObjectUtil.isNotNull(baseThreeMeantime.getConstructDate()),BaseThreeMeantimeEntity::getConstructDate,baseThreeMeantime.getConstructDate());
        wrapper.le(ObjectUtil.isNotNull(baseThreeMeantime.getCompleteDate()),BaseThreeMeantimeEntity::getCompleteDate,baseThreeMeantime.getCompleteDate());
        wrapper.like(ObjectUtil.isNotNull(baseThreeMeantime.getProjectName()),BaseThreeMeantimeEntity::getProjectName,baseThreeMeantime.getProjectName());
        wrapper.orderByDesc(BaseThreeMeantimeEntity::getOrgId);
        return R.ok(baseThreeMeantimeService.page(page, wrapper));
    }

    /**
     * 申报界面分页查询
     * @param page 分页对象
     * @param baseThreeMeantime 项目三同时管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/declarePage" )
    @PreAuthorize("@pms.hasPermission('safe_meantime_view')" )
    public R getDeclarePage(Page page, BaseThreeMeantimeEntity baseThreeMeantime) {
        LambdaQueryWrapper<BaseThreeMeantimeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseThreeMeantime.getOrgId()),BaseThreeMeantimeEntity::getOrgId,baseThreeMeantime.getOrgId());
        wrapper.eq(ObjectUtil.isNotNull(baseThreeMeantime.getProjectStatus()),BaseThreeMeantimeEntity::getProjectStatus,baseThreeMeantime.getProjectStatus());
        wrapper.eq(ObjectUtil.isNotNull(baseThreeMeantime.getProjectType()),BaseThreeMeantimeEntity::getProjectType,baseThreeMeantime.getProjectType());
        wrapper.ge(ObjectUtil.isNotNull(baseThreeMeantime.getConstructDate()),BaseThreeMeantimeEntity::getConstructDate,baseThreeMeantime.getConstructDate());
        wrapper.le(ObjectUtil.isNotNull(baseThreeMeantime.getCompleteDate()),BaseThreeMeantimeEntity::getCompleteDate,baseThreeMeantime.getCompleteDate());
        wrapper.like(ObjectUtil.isNotNull(baseThreeMeantime.getProjectName()),BaseThreeMeantimeEntity::getProjectName,baseThreeMeantime.getProjectName());
        wrapper.and(item->item.eq(BaseThreeMeantimeEntity::getProjectStatus, FlowDicConstant.NOT_PASS).or().eq(BaseThreeMeantimeEntity::getProjectStatus, FlowDicConstant.FILL_IN));
        wrapper.orderByDesc(BaseThreeMeantimeEntity::getOrgId);
        return R.ok(baseThreeMeantimeService.page(page, wrapper));
    }


    /**
     * 通过id查询项目三同时管理
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('safe_meantime_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(baseThreeMeantimeService.getById(id));
    }

    /**
     * 新增项目三同时管理
     * @param baseThreeMeantime 项目三同时管理
     * @return R
     */
    @Operation(summary = "新增项目三同时管理" , description = "新增项目三同时管理" )
    @SysLog("新增项目三同时管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('safe_meantime_add')" )
    public R save(@RequestBody BaseThreeMeantimeEntity baseThreeMeantime) {
        baseThreeMeantime.setProjectStatus(FlowDicConstant.FILL_IN);
        return R.ok(baseThreeMeantimeService.save(baseThreeMeantime));
    }

    /**
     * 修改项目三同时管理
     * @param baseThreeMeantime 项目三同时管理
     * @return R
     */
    @Operation(summary = "修改项目三同时管理" , description = "修改项目三同时管理" )
    @SysLog("修改项目三同时管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('safe_meantime_edit')" )
    public R updateById(@RequestBody BaseThreeMeantimeEntity baseThreeMeantime) {
        return R.ok(baseThreeMeantimeService.updateById(baseThreeMeantime));
    }

    /**
     * 通过id删除项目三同时管理
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除项目三同时管理" , description = "通过id删除项目三同时管理" )
    @SysLog("通过id删除项目三同时管理" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('safe_meantime_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(baseThreeMeantimeService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('safe_meantime_export')")
    public List<BaseThreeMeantimeExcelVO> template() {
        return baseThreeMeantimeService.exportExcel(Collections.singletonList(new BaseThreeMeantimeEntity()));
    }

    /**
     * 导出excel 表格
     * @param baseThreeMeantimeExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('safe_meantime_export')")
    public List<BaseThreeMeantimeExcelVO> export(BaseThreeMeantimeExcelDTO baseThreeMeantimeExcelDto) {
        LambdaQueryWrapper<BaseThreeMeantimeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper
                .eq(StringUtils.isNotBlank(baseThreeMeantimeExcelDto.getOrgId()),BaseThreeMeantimeEntity::getOrgId,baseThreeMeantimeExcelDto.getOrgId())
                .eq(StringUtils.isNotBlank(baseThreeMeantimeExcelDto.getProjectType()),BaseThreeMeantimeEntity::getProjectType,baseThreeMeantimeExcelDto.getProjectType())
                .eq(StringUtils.isNotBlank(baseThreeMeantimeExcelDto.getProjectStatus()),BaseThreeMeantimeEntity::getProjectStatus,baseThreeMeantimeExcelDto.getProjectStatus())
                .like(StringUtils.isNotBlank(baseThreeMeantimeExcelDto.getProjectName()),BaseThreeMeantimeEntity::getProjectName,baseThreeMeantimeExcelDto.getProjectName());
        return baseThreeMeantimeService.exportExcel(baseThreeMeantimeService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param baseThreeMeantimeExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('safe_meantime_export')")
    public R importExcel(@RequestExcel List<BaseThreeMeantimeExcelDTO> baseThreeMeantimeExcelDtoList, BindingResult bindingResult) {
        return baseThreeMeantimeService.importExcel(baseThreeMeantimeExcelDtoList, bindingResult);
    }

    @Operation(summary = "查询我的待审批", description = "查询我的待审批")
    @GetMapping("/queryTodoForPage")
    public R queryTodoForPage(Page page, BaseThreeMeantimeFlowSearchDTO flowSearch) {
        flowSearch.setTaskKey(FlowDicConstant.IN_REVIEW);
        Page res = baseThreeMeantimeService.queryTodoForPage(page, flowSearch);
        return R.ok(res);
    }

    @Operation(summary = "提交审批结果", description = "提交审批结果")
    @SysLog("提交审批结果")
    @PostMapping("/handlerAudit")
    public R handlerAudit(@Validated @RequestBody BaseAuditDTO baseAuditDTO) {
        baseThreeMeantimeService.handlerAudit(baseAuditDTO);
        return R.ok();
    }

    @Operation(summary = "提交", description = "提交")
    @SysLog("提交")
    @GetMapping("/threeMeantimeSubmit/{id}")
    public R threeMeantimeSubmit(@PathVariable("id")  Long id) {
        baseThreeMeantimeService.handleThreeMeantimeSubmit(id);
        return R.ok();
    }

    /**
     * 新增项目进度处理
     * @param baseThreeMeantimeHandleRecord 新增项目进度处理
     * @return R
     */
    @Operation(summary = "新增项目进度处理" , description = "新增项目进度处理" )
    @SysLog("新增项目进度处理" )
    @PostMapping("savePlan")
    public R savePlan(@RequestBody BaseThreeMeantimeHandleRecordEntity baseThreeMeantimeHandleRecord) {
        baseThreeMeantimeService.savePlan(baseThreeMeantimeHandleRecord);
        return R.ok();
    }
}
